package com.jims.master.modules.nurse.web;

import com.jims.master.common.data.StringData;
import com.jims.master.common.persistence.Page;
import com.jims.master.common.utils.StringUtils;
import com.jims.master.common.web.BaseController;
import com.jims.master.modules.doctor.entity.OutpTreatPresc;
import com.jims.master.modules.doctor.service.OutpTreatPrescService;
import com.jims.master.modules.sys.service.OrgDeptService;
import com.jims.master.modules.sys.utils.UserUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Created by zhangpeng on 2017/7/10.
 */

@Controller
@RequestMapping(value = "${adminPath}/OutpTreatPresc/OutpTreatPresc")
public class OutpTreatPrescController extends BaseController {
    @Autowired
    private OutpTreatPrescService outpTreatPrescService;
    @Autowired
    private OrgDeptService orgDeptService;

    @ModelAttribute
    public OutpTreatPresc get(@RequestParam(required=false) String id) {
        OutpTreatPresc entity = null;
        if (StringUtils.isNotBlank(id)){
            entity = outpTreatPrescService.get(id);
        }
        if (entity == null){
            entity = new OutpTreatPresc();
        }
        return entity;
    }

    @RequiresPermissions("user")
    @RequestMapping(value = {"index", ""})
    public String list(OutpTreatPresc outpTreatPresc, HttpServletRequest request, HttpServletResponse response, Model model) {
        UserUtils userUtils = new UserUtils();
        Page<OutpTreatPresc> page = new Page<OutpTreatPresc>();
        String attr = orgDeptService.get(UserUtils.getUser().getOrgDept().getId()).getClinicAttr();
        if("10".equals(attr)){
            //查询这个病人已开的处置治疗
             page = outpTreatPrescService.findPage(new Page<OutpTreatPresc>(request, response), outpTreatPresc);
        }
        model.addAttribute("page", page);
        return "modules/nurse/skinTestResult";
    }

    @RequiresPermissions("user")
    @RequestMapping(value = "updateResult")
    @ResponseBody
    public StringData updateResult(String id,String skinResult){
        StringData data = new StringData();
        OutpTreatPresc outpTreatPresc = new OutpTreatPresc();
        outpTreatPresc.setId(id);
        outpTreatPresc.setSkinResult(skinResult);
        outpTreatPresc.updateOrgId();
        int num = outpTreatPrescService.updateResult(outpTreatPresc);
        if(num>0){
            data.setCode("success");
            data.setData("结果保存成功");
        }else{
            data.setCode("error");
            data.setData("结果保存失败");
        }
        return data;

    }
}
